/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *MmsSuggestionFeedbackController
 * 意见反馈表
 *
 * 修改纪录
 * 2021-11-01 版本：1.0 zhangqianbo 创建。
 * @version 版本：1.0
 * @author 作者：zhangqianbo
 * 创建日期：2021-11-01
 */

package com.topscomm.mms.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.topscomm.cbo.pojo.CboRoleEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.ICboRoleService;
import com.topscomm.cbo.service.ICboUserService;
import com.topscomm.cbo.service.impl.CboItemDetailsService;
import com.topscomm.mms.MmsSystemConst;
import com.topscomm.mms.pojo.MmsResponseEntity;
import com.topscomm.mms.pojo.MmsSuggestionFeedbackEntity;
import com.topscomm.mms.service.IMmsBaseConfigService;
import com.topscomm.mms.service.IMmsResponseService;
import com.topscomm.mms.service.impl.MmsResponseService;
import com.topscomm.pub.util.*;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.sys.SysSystemConst;
import com.topscomm.tap.common.ParameterCache;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.topscomm.mms.controller.auto.MmsSuggestionFeedbackControllerAuto;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: zhangqianbo
 * @date: 2021-11-01
 * @description:意见反馈表Controller
 * @modify:
 */
@RestController
@RequestMapping("mms/suggestionFeedback")
public class MmsSuggestionFeedbackController extends MmsSuggestionFeedbackControllerAuto implements IMmsSuggestionFeedbackController {

	@Autowired
	public IMmsResponseService mmsResponseService;

	@Autowired
	public ICboRoleService cboRoleService;

	@Autowired
	public IMmsBaseConfigService mmsBaseConfigService;

	@Autowired
	public ICboUserService cboUserService;


	/**
	 * @author: zhangqianbo
	 * @date: 2021-11-01
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @description: 提交审批前
	 * @param dataMap
	 * @param paraMap
	 * @throws Exception
	 * @author:zhangqianbo
	 * @date:
	 * @modify:
	 */
	@Override
	protected void beforeSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeSubmit(dataMap, paraMap);
		StringBufferProxy mailtitle = new StringBufferProxy();
		mailtitle.appendSingle("{0}于{1}提交意见反馈！",paraMap.get(MmsSuggestionFeedbackEntity.FieldApplyusername),
				DateUtil.dateToString(new Date(), "yyyy-MM-dd"));
		dataMap.put("maintitle", mailtitle.toString());
	}
	/**
	 *
	 * @description:
	 * @param paraMap
	 * @return
	 * @author:zhangqianbo
	 * @date:
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/dataCodeList")
	public ResponseResult<Object> dataCodeList(@RequestParam(required = false) Map<String, Object> paraMap) {
		Map<String, Object> codeMap = new HashMap<String, Object>();
		// 查询CboItemDetails表中存储的值
		List<Map<String, Object>> feedbackTypeList = datacodeCache.getCodeListMap("FeedbackType", "", "sortcode");
		codeMap.put("feedbackTypeList", feedbackTypeList);
		List<Map<String, Object>> feedbackServiceTypeList = datacodeCache.getCodeListMap("FeedbackServiceType", "", "sortcode");
		codeMap.put("feedbackServiceTypeList", feedbackServiceTypeList);
		List<Map<String, Object>> feedbackSystemList = datacodeCache.getCodeListMap("FeedbackSystem", "", "sortcode");
		codeMap.put("feedbackSystemList", feedbackSystemList);
		List<Map<String, Object>> feedbackModuleList = datacodeCache.getCodeListMap("FeedbackModule", "", "sortcode");
		codeMap.put("feedbackModuleList", feedbackModuleList);
		List<Map<String, Object>> feedbackIsPublicList = datacodeCache.getCodeListMap("FeedbackIsPublic", "", "sortcode");
		codeMap.put("feedbackIsPublicList", feedbackIsPublicList);
		List<Map<String, Object>> feedbackQuestionLevelList = datacodeCache.getCodeListMap("FeedbackQuestionLevel", "", "sortcode");
		codeMap.put("feedbackQuestionLevelList", feedbackQuestionLevelList);
		List<Map<String, Object>> enabledList = datacodeCache.getCodeListMap("CboYesNo", "", "sortcode");
		codeMap.put("enabledList", enabledList);
		List<Map<String, Object>> businessTypeList = datacodeCache.getCodeListMap("BusinessTypeList", "", "sortcode");
		codeMap.put("businessTypeList", businessTypeList);
		super.dataCodeList(paraMap);
		return ResponseResult.ok(codeMap);
	}
	/**
	 *
	 * @description:
	 * @param paraMap
	 * @return
	 * @author:zhangqianbo
	 * @date:
	 * @modify:
	 */
	@GetMapping(value = "/feedbackInfo")
	public ResponseResult<Object> feedbackInfo(@RequestParam(required = false) Map<String, Object> paraMap) {
		String id = ConvertUtil.convertToString(paraMap.get("qry_id"));
		MmsSuggestionFeedbackEntity mmsSuggestionFeedbackEntity = this.service.queryById(id);
		return ResponseResult.ok(mmsSuggestionFeedbackEntity);
	}
	/**
	 *
	 * @description:待办列表
	 * @param paraMap
	 * @return
	 * @author:zhangqianbo
	 * @date:
	 * @modify:
	 */
	@GetMapping(value = "/queryMyApprovalList")
	public ResponseResult<Object> queryMyApprovalList(@RequestParam(required = false) Map<String, Object> paraMap) {

		paraMap = BaseUtil.decodeSecureMap(paraMap);
		Query query = new Query(paraMap);
		List<Map<String, Object>> tempList = null;
		// 查询结点状态为1的数据
		paraMap.put("qry_state", "1");
		int totalCount = this.service.getCount1(paraMap, "queryMyApprovalList");
		if (query.getPageSize() > 0) {
			paraMap.put("IsNeedPage", true);
			// 分页拦截处理
			int beginCount = query.getCurrentPage() * query.getPageSize() - query.getPageSize();
			paraMap.put("begincount", beginCount);
			paraMap.put("pagesize", query.getPageSize());
			tempList = this.service.queryMapForPage(paraMap, "queryMyApprovalList");
		} else {
			tempList = this.service.queryMap(paraMap, "queryMyApprovalList");
		}
		this.service.setDispFields(tempList);
		Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
		return ResponseResult.ok(page);
	}

	/**
	 *
	 * @description:反馈列表展示
	 * @param paraMap
	 * @return
	 * @author:zhangqianbo
	 * @date:
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/list")
	public ResponseResult<Object> list(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Query query = new Query(paraMap);
			List<Map<String, Object>> tempList = new ArrayList<>();
			int totalCount = 0;
			String refListName = ConvertUtil.convertToString(paraMap.get("refName"));
			if ("feedbackHistory".equals(refListName)) {
				// 查询办理历史
				totalCount = this.service.queryFeedbackHistory(paraMap, tempList);
			} else if ("feedbackList".equals(refListName)) {
				// 反馈列表
				totalCount = this.service.queryFeedbackList(paraMap, tempList);
			} else {
				// 我的反馈和反馈广场列表
				totalCount = this.service.queryMyFeedback(paraMap, tempList);
			}

			if (!CollectionUtils.isEmpty(tempList)) {
				// 查询回复表sql
				this.service.queryFeedbackResponse(tempList);
			}

			this.service.setDispFields(tempList);
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 *
	 * @description: 处理审批页面展示值
	 * @param paraMap
	 * @return
	 * @author:zhangqianbo
	 * @date:
	 * @modify:
	 */
	@Override
	protected void afterLoad(Map<String, Object> paraMap) {
		super.afterLoad(paraMap);
		// 处理所属负责人显示值
		String systemResponsibleName = ConvertUtil.convertToString(paraMap.get("systemresponsiblename"));
		String systemResponsibleCode = ConvertUtil.convertToString(paraMap.get("systemresponsiblecode"));
		paraMap.put("dispsystemperson", systemResponsibleName + "[" + systemResponsibleCode + "]");

		// 反馈人显示值
		String submitPersonName = ConvertUtil.convertToString(paraMap.get("submitpersonname"));
		String submitPerson = ConvertUtil.convertToString(paraMap.get("submitperson"));
		paraMap.put("dispsubmitperson", submitPersonName + "[" + submitPerson + "]");

		// 获取系统负责人电话
		StringBufferProxy sqlWhere = new StringBufferProxy();
		sqlWhere.appendSingle("code = '{0}'", systemResponsibleCode);
		CboUserEntity cboUserEntity = this.cboUserService.queryObjectByWhere(sqlWhere.toString());
		if (cboUserEntity != null) {
			String mobile = cboUserEntity.getMobile();
			paraMap.put("systemresponsibletel", mobile);
		}
	}


	/**
	 *
	 * @description: 查询当前用户的角色
	 * @param paraMap
	 * @return
	 * @author:zhangqianbo
	 * @date:
	 * @modify:
	 */
	@GetMapping(value = "/queryUserRole")
	public ResponseResult<Object> queryUserRole(@RequestParam(required = false) Map<String, Object> paraMap) {
		// 当前用户
		String userCode = this.getUserBean().getUser().getCode();
		// 是否为意见反馈管理员
		paraMap.put("userCode", userCode);
		List<Map<String, Object>> managerList = this.service.queryMap(paraMap, "queryFeedbackManager");
		if (managerList.size() > 0) {
			for (Map<String, Object> tempMap : managerList) {
				tempMap.put("role", "manager");
				tempMap.put("system", "");
			}
			return ResponseResult.ok(managerList);
		}

		// 是否为系统负责人
		StringBufferProxy sqlWhere = new StringBufferProxy();
		sqlWhere.appendSingle("',' || systemresponsiblecode || ',' like  '%,{0},%'", userCode);
		List<Map<String, Object>> systemResponsibleList = this.mmsBaseConfigService.queryMapByWhere(sqlWhere.toString());
		if (systemResponsibleList.size() > 0) {
			for (Map<String, Object> tempMap : systemResponsibleList) {
				tempMap.put("role", "systemresponsible");
			}
			return ResponseResult.ok(systemResponsibleList);
		}
		return ResponseResult.ok("");
	}



	/**
	 *
	 * @description:待办列表
	 * @param paraMap
	 * @return
	 * @author:zhangqianbo
	 * @date:
	 * @modify:
	 */
	@GetMapping(value = "/queryMyApprovalListNoToken")
	public ResponseResult<Object> queryMyApprovalListNoToken(@RequestParam(required = false) Map<String, Object> paraMap) {

		paraMap = BaseUtil.decodeSecureMap(paraMap);
		Query query = new Query(paraMap);
		List<Map<String, Object>> tempList = null;
		// 当前用户
		String userCode = this.getUserBean().getUser().getCode();
		paraMap.put("qry_code", userCode);

		// 查询结点状态为1的数据
		paraMap.put("qry_state", "1");
		int totalCount = this.service.getCount1(paraMap, "queryMyApprovalList");
		if (query.getPageSize() > 0) {
			paraMap.put("IsNeedPage", true);
			// 分页拦截处理
			int beginCount = query.getCurrentPage() * query.getPageSize() - query.getPageSize();
			paraMap.put("begincount", beginCount);
			paraMap.put("pagesize", query.getPageSize());
			tempList = this.service.queryMapForPage(paraMap, "queryMyApprovalList");
		} else {
			tempList = this.service.queryMap(paraMap, "queryMyApprovalList");
		}
		this.service.setDispFields(tempList);
		Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
		return ResponseResult.ok(page);
	}
}

